namespace AspireFyh.Web;

public class WeatherApiClient(HttpClient httpClient)
{
    // public async Task<WeatherForecast[]> GetWeatherAsync(int maxItems = 10, CancellationToken cancellationToken = default)
    // {
    //     List<WeatherForecast>? forecasts = null;
    //
    //     await foreach (var forecast in httpClient.GetFromJsonAsAsyncEnumerable<WeatherForecast>("/weatherforecast", cancellationToken))
    //     {
    //         if (forecasts?.Count >= maxItems)
    //         {
    //             break;
    //         }
    //         if (forecast is not null)
    //         {
    //             forecasts ??= [];
    //             forecasts.Add(forecast);
    //         }
    //     }
    //
    //     return forecasts?.ToArray() ?? [];
    // }

    public async Task<Account[]> GetAccounts(int maxItems = 10, CancellationToken cancellationToken = default)
    {
        var accounts = new List<Account>();
        await foreach (var account in httpClient.GetFromJsonAsAsyncEnumerable<Account>("/api/account", cancellationToken))
        {
            if (accounts.Count >= maxItems)
            {
                break;
            }
            if (account is not null)
            {
                accounts.Add(new Account(account.Username, account.Phone));
            }
        }

        return accounts.ToArray() ?? [];
    }
}

public record Account(string? Username, string? Phone)
{
}

